package cn.iocoder.yudao.module.cdk.controller.admin.cdkuser;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkuser.vo.CdkuserPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkuser.vo.CdkuserRespVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkuser.vo.CdkuserSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdkuser.CdkuserDO;
import cn.iocoder.yudao.module.cdk.service.cdkuser.CdkuserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdk使用用户")
@RestController
@RequestMapping("/cdk/user")
@Validated
public class CdkuserController {

    @Resource
    private CdkuserService userService;

    @RateLimiter(count = 20, timeUnit = TimeUnit.MINUTES)
    @PostMapping("/create")
    @Operation(summary = "创建cdk使用用户")
    public CommonResult<Integer> createuser(@Valid @RequestBody CdkuserSaveReqVO createReqVO) {
        return success(userService.createuser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cdk使用用户")
    @PreAuthorize("@ss.hasPermission('cdk:user:update')")
    public CommonResult<Boolean> updateuser(@Valid @RequestBody CdkuserSaveReqVO updateReqVO) {
        userService.updateuser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cdk使用用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cdk:user:delete')")
    public CommonResult<Boolean> deleteuser(@RequestParam("id") Integer id) {
        userService.deleteuser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cdk使用用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cdk:user:query')")
    public CommonResult<CdkuserRespVO> getuser(@RequestParam("id") Integer id) {
        CdkuserDO user = userService.getuser(id);
        return success(BeanUtils.toBean(user, CdkuserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得cdk使用用户分页")
    @PreAuthorize("@ss.hasPermission('cdk:user:query')")
    public CommonResult<PageResult<CdkuserRespVO>> getuserPage(@Valid CdkuserPageReqVO pageReqVO) {
        PageResult<CdkuserDO> pageResult = userService.getuserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CdkuserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cdk使用用户 Excel")
    @PreAuthorize("@ss.hasPermission('cdk:user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportuserExcel(@Valid CdkuserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CdkuserDO> list = userService.getuserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "cdk使用用户.xls", "数据", CdkuserRespVO.class,
                        BeanUtils.toBean(list, CdkuserRespVO.class));
    }

}
